package com.itheima.edu.info.temanager.controller;

import com.itheima.edu.info.temanager.domain.Teacher;
import com.itheima.edu.info.temanager.service.TeacherService;

import java.util.Scanner;

public class TeacherController {
    private TeacherService teacherService = new TeacherService();

    public void start() {

        Scanner sc = new Scanner(System.in);
        while (true) {
            //给出表单信息
            System.out.println("--------欢迎来到 <老师> 管理系统--------");
            System.out.println("请输入您的选择: 1.添加老师  2.删除老师  3.修改老师  4.查看老师  5.退出");
            //又是一串switchcase
            String choice = sc.next();
            switch (choice) {
                case "1":
                    //System.out.println("添加");
                    addTeacher();
                    break;
                case "2":
                    System.out.println("删除");
                    break;
                case "3":
                    System.out.println("修改");
                    break;
                case "4":
                    //System.out.println("查看");
                    findAllTeacher();
                    break;
                case "5":
                    System.out.println("感谢您使用<老师> 管理系统");
                    return;
                default:
                    System.out.println("您输入的数据有误,请重新输入:");
                    break;
            }
        }
    }

    private void findAllTeacher() {
        //1.让TeacherService对象调用findAllTeacher()方法,
        Teacher[] teachers = teacherService.findAllTeacher();
        //2.看传回来的teachers数组是什么东西
        //如果是null
        if (teachers == null) {
            System.out.println("查无信息,请先添加信息");
        } else {
            System.out.println("学号\t\t姓名\t年龄\t生日");
            for (int i = 0; i < teachers.length; i++) {
                Teacher teacher = teachers[i];
                if (teacher != null) {
                    System.out.println(teacher.getId() + "\t" + teacher.getName() + "\t" + teacher.getAge() + "\t\t" + teacher.getBirthday());
                }
            }
        }
    }

    private void addTeacher() {
        Scanner sc = new Scanner(System.in);
        //1.创建一个TeacherService对象调用addTeacher方法接收一个Student数组对象
        //TeacherService teacherService = new TeacherService();
        String id;
        while (true) {
            System.out.println("请输入老师工号");
            id = sc.next();
            boolean flag = teacherService.isExists(id);
            if (flag) {
                //flag=true
                System.out.println("您输入的工号已存在,请重新输入");
            } else {
                //不相同则接着输出
                break;
            }
        }

        System.out.println("请输入老师姓名");
        String name = sc.next();
        System.out.println("请输入老师年龄");
        String age = sc.next();
        System.out.println("请输入老师生日");
        String birthday = sc.next();

        Teacher te = new Teacher();
        te.setId(id);
        te.setName(name);
        te.setAge(age);
        te.setBirthday(birthday);
        //将封装的教师对象传递给teacherService对象
        boolean flag = teacherService.addTeacher(te);

        //根据返回的值给用户信息
        if (flag) {
            System.out.println("添加成功");
        } else {
            System.out.println("添加失败,请重新添加");
        }

    }
}
